package Dragons;

import java.util.ArrayList;

import jgame.JGPoint;
import jgame.JGTimer;
import Main.Board;
import Main.Game;
import Other.Field;
import Robots.RobotKing;
import Units.Dragon;
import Units.UnitName;

public class DragonKing extends Dragon {
	public static int imageHeight = 99, imageWidth = 89;
	
	private final static String stringStandingDown = "dragon5",
								stringGoDown	   = "robot1animgo_down",
								stringStopDown	   = "robot1animstop_down",
								stringMoveDown	   = "robot1animmove_down";
	
	public DragonKing(String name, boolean unique, double posX, double posY, int cid){
		super(name, unique, posX, posY, cid, stringStandingDown);
	}

	/*@Override
	public boolean canMove(Field field) {
		if (!Game.board.inBoard(field.i, field.j)) return false;
		if ( Game.board.getUnitName(field.i, field.j) != UnitName.EMPTY ) return false;
		
		boolean can = false;
		for (int dir=0; dir<Field.adjacentFields.length; dir++) {
			if (field.i == this.getField().i + Field.adjacentFields[dir][0] && field.j == this.getField().j + Field.adjacentFields[dir][1]) can = true;
		}
		return can;
	}*/
	@Override
	public boolean canMove(Field field) {
		if ( Game.board.getUnitName(field.i, field.j) != UnitName.EMPTY ) return false;
		boolean can = false;
		for (int dir=0; dir<Field.adjacentFields.length; dir++) {
			if (field.i == this.getField().i + Field.adjacentFields[dir][0] && field.j == this.getField().j + Field.adjacentFields[dir][1]) can = true;
		}
		return can;
	}
	
	@Override
	public ArrayList<Field> whereAttack(Field field) {
			
		ArrayList<Field> ret = new ArrayList<Field>();
		
		ret.clear();
		
		if (!Game.board.inBoard(field.i, field.j)) return ret;
				
		for (int dir=0; dir<Field.adjacentFields.length; dir++) {
			
			Field firstMove = new Field(this.getField().i + Field.adjacentFields[dir][0], 
										this.getField().j + Field.adjacentFields[dir][1]);
			
			Field secondMove = new Field(firstMove.i + Field.adjacentFields[dir][0], 
										 firstMove.j + Field.adjacentFields[dir][1]);
			
			if (field.compare(secondMove)){
				int directionLeft = (dir - 1 + Field.adjacentFields.length) % Field.adjacentFields.length;
				int directionRight = (dir + 1 + Field.adjacentFields.length) % Field.adjacentFields.length;
				
				Field left = new Field(firstMove.i + Field.adjacentFields[directionLeft][0], 
									   firstMove.j + Field.adjacentFields[directionLeft][1]);
				Field right = new Field(firstMove.i + Field.adjacentFields[directionRight][0], 
						   			    firstMove.j + Field.adjacentFields[directionRight][1]);
				
				boolean validShot = false;
				
				Board b = Game.board;
				
				if (b.getUnitName(firstMove.i, firstMove.j) != UnitName.EMPTY
						&& b.getUnitName(firstMove.i, firstMove.j) != UnitName.NULL) validShot=true;
				if (b.getUnitName(secondMove.i, secondMove.j) != UnitName.EMPTY
						&& b.getUnitName(secondMove.i, secondMove.j) != UnitName.NULL) validShot=true;
				if (b.getUnitName(left.i, left.j) != UnitName.EMPTY
						&& b.getUnitName(left.i, left.j) != UnitName.NULL) validShot=true;
				if (b.getUnitName(right.i, right.j) != UnitName.EMPTY
						&& b.getUnitName(right.i, right.j) != UnitName.NULL) validShot=true;
				
				if (validShot){
					if (b.getUnitName(firstMove.i, firstMove.j)!=UnitName.NULL) ret.add(firstMove);
					if (b.getUnitName(secondMove.i, secondMove.j)!=UnitName.NULL) ret.add(secondMove);
					if (b.getUnitName(left.i, left.j)!=UnitName.NULL) ret.add(left);
					if (b.getUnitName(right.i, right.j)!=UnitName.NULL) ret.add(right);
				}
			}
					
		}
		
		return ret;
	}
	
	@Override
	public boolean canAttack(Field field) {
		// TODO Auto-generated method stub
		if (!Game.board.inBoard(field.i, field.j)) return false;
		
		boolean validShot = false;
		
		for (int dir=0; dir<Field.adjacentFields.length; dir++) {
			
			Field firstMove = new Field(this.getField().i + Field.adjacentFields[dir][0], 
										this.getField().j + Field.adjacentFields[dir][1]);
			
			Field secondMove = new Field(firstMove.i + Field.adjacentFields[dir][0], 
										 firstMove.j + Field.adjacentFields[dir][1]);
			
			if (field.compare(secondMove)){
				int directionLeft = (dir - 1 + Field.adjacentFields.length) % Field.adjacentFields.length;
				int directionRight = (dir + 1 + Field.adjacentFields.length) % Field.adjacentFields.length;
				
				Field left = new Field(firstMove.i + Field.adjacentFields[directionLeft][0], 
									   firstMove.j + Field.adjacentFields[directionLeft][1]);
				Field right = new Field(firstMove.i + Field.adjacentFields[directionRight][0], 
						   			    firstMove.j + Field.adjacentFields[directionRight][1]);
				
				
				
				Board b = Game.board;
				
				if (b.getUnitName(firstMove.i, firstMove.j) != UnitName.EMPTY
						&& b.getUnitName(firstMove.i, firstMove.j) != UnitName.NULL) validShot=true;
				if (b.getUnitName(secondMove.i, secondMove.j) != UnitName.EMPTY
						&& b.getUnitName(secondMove.i, secondMove.j) != UnitName.NULL) validShot=true;
				if (b.getUnitName(left.i, left.j) != UnitName.EMPTY
						&& b.getUnitName(left.i, left.j) != UnitName.NULL) validShot=true;
				if (b.getUnitName(right.i, right.j) != UnitName.EMPTY
						&& b.getUnitName(right.i, right.j) != UnitName.NULL) validShot=true;
			}
					
		}
		
		return validShot;
	}

	public void move() {
		switch (getState()){
			case 0:
				setGraphic(stringStandingDown);
				break;
			case 1:
				setGraphic(stringGoDown);
				break;
			case 2:
				setGraphic(stringMoveDown);
				JGPoint p = Game.board.getRealCoordinates(getTargetField());
				
				if (Game.board.distance(p, new JGPoint((int)getLastX() + this.imageWidth/2, (int)getLastY() + this.imageHeight/2)) < 5){
					xspeed = yspeed = 0;
					setState(3);
					new JGTimer(5, true){

						@Override
						public void alarm() {
							setState(0);
							Game.board.setAnimation(false);
							Field f = DragonKing.this.getField();
							Field t = DragonKing.this.getTargetField();
							
							Game.board.moveFromTo(f, t);
							DragonKing.this.setField(DragonKing.this.getTargetField());
						}
					};
				}
				break;
			case 3:
				setGraphic(stringStopDown);
				break;
			default: setGraphic(stringStandingDown);
		}		
	}
}
